package com.example.smartcity.ui.dashboard;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.AllFunctionAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentFunction#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentFunction extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_title = "title";
    private static final String ARG_icon = "icon";
    private TextView FunctionTitle;
    private GridView functionItem;
    // TODO: Rename and change types of parameters
    private String[] title;
    private int[] icon;

    public FragmentFunction() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentFunction.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentFunction newInstance(String param1, String param2) {
        FragmentFunction fragment = new FragmentFunction();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getStringArray(ARG_title);
            icon = getArguments().getIntArray(ARG_icon);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_function, container, false);
        FunctionTitle = view.findViewById(R.id.FunctionTitle);
        functionItem = view.findViewById(R.id.functionItem);
        initView();
        return view;
    }
    public void initView(){
        FunctionTitle.setText(getArguments().getString("functionMenu"));
        functionItem.setAdapter(new AllFunctionAdapter(title,icon,R.layout.topicefunctionitem));
        functionItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = view.findViewById(R.id.TV_topicFunctionTitle);
                 switch (textView.getText().toString()){
                     case "停车场":
                         startActivity(new Intent(FragmentFunction.this.getContext(),stopCar.class));
                         break;
                     case "智慧巴士":
                         startActivity(new Intent(FragmentFunction.this.getContext(),SmartBar.class));
                         break;
                     case "城市地铁":
                         startActivity(new Intent(FragmentFunction.this.getContext(),CitySubway.class));
                         break;
                     case "门诊预约":
                         startActivity(new Intent(FragmentFunction.this.getContext(),OrderHospital.class));
                         break;
                 }
            }
        });
    }
}